|
1
|
|
|
/** |
|
2
|
|
|
* The list of registered components. |
|
3
|
|
|
* |
|
4
|
|
|
* @type Array |
|
5
|
|
|
*/ |
|
6
|
|
|
Mivhak.components = []; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Register a new component |
|
10
|
|
|
* |
|
11
|
|
|
* @param {string} name The components name |
|
12
|
|
|
* @param {Object} options A list of component properties |
|
13
|
|
|
*/ |
|
14
|
|
|
Mivhak.component = function(name, options) |
|
15
|
|
|
{ |
|
16
|
|
|
Mivhak.components[name] = options; |
|
17
|
|
|
}; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Render a new component |
|
21
|
|
|
* |
|
22
|
|
|
* TODO: move this into a seperate library |
|
23
|
|
|
* |
|
24
|
|
|
* @param {string} name The components name |
|
25
|
|
|
* @param {Object} props A list of component properties. |
|
26
|
|
|
* This overrides the component's initial property values. |
|
27
|
|
|
*/ |
|
28
|
|
|
Mivhak.render = function(name, props) |
|
29
|
|
|
{ |
|
30
|
|
|
var component = $.extend(true, {}, Mivhak.components[name]); |
|
31
|
|
|
var el = {}; |
|
32
|
|
|
|
|
33
|
|
|
// Create the element from the template |
|
34
|
|
|
el.$el = $(component.template); |
|
35
|
|
|
|
|
36
|
|
|
// Create all methods |
|
37
|
|
|
$.each(component.methods, function(name, method){ |
|
38
|
|
|
el[name] = function() {return method.apply(el,arguments);}; |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
// Set properties |
|
42
|
|
|
$.each(component.props, function(name, prop){ |
|
43
|
|
|
el[name] = (typeof props !== 'undefined' && props.hasOwnProperty(name) ? props[name] : prop); |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
// Bind events |
|
47
|
|
|
$.each(component.events, function(name, method){ |
|
48
|
|
|
el.$el.on(name, function() {return method.apply(el,arguments);}); |
|
49
|
|
|
}); |
|
50
|
|
|
|
|
51
|
|
|
// Call the 'created' function if exists |
|
52
|
|
|
if(component.hasOwnProperty('created')) component.created.call(el); |
|
53
|
|
|
|
|
54
|
|
|
return el; |
|
55
|
|
|
}; |